namespace OpenPms.Areas.D.Pages.Setup.WXMsg;

public class IndexModel : PageModel
{
    private readonly AppDbContext db;
    public IndexModel(AppDbContext db)
    {
        this.db = db;
    }
    public TWXMsg Item { get; set; } = default!;
    public List<TWXMsg> Items { get; set; } = default!;
    public async Task OnGet()
    {
        var values = string.Join(",", Enum.GetValues<EWXMsgType>().Select(ii => $"({(int)ii})"));
        var sql = $@"with tt as(select * from wxmsgs where hotelid={{0}})
merge tt
using (values {values}
) as ts(msgtype)
on tt.wxmsgtype=ts.msgtype
when not matched then insert (hotelid,wxmsgtype,corpId) values ({{0}},ts.msgtype,{{1}});
";
        await db.Database.ExecuteSqlRawAsync(sql, User.GetExtId(), User.GetCorpId());
        Items = await db.WXMsgs.Where(ii => ii.HotelId == User.GetExtId()).ToListAsync();
    }
}
